<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <c:set var="ctx" value="${pageContext.request.contextPath}"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>业务数据流程权限关系</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <c:if test="${not empty buOrgCdJson}">
        <form id="formSet1">
            <c:forEach var="buOrgCdNm" items="${buOrgCdJson}" begin="0" step="1" varStatus="status">
                <div class="fieldset1 tabel_col clearFix">
                    <div class="fieldBox col_100_auto">
                        <div class="label" style="width: auto;">${buOrgCdNm.value}<br> 可支持流程</div>
                        <div class="field">
                            <input id="ALL${buOrgCdNm.key}" type="checkbox" value="ALL" name="ALL"
                                   <c:if test="${saveBizFlowRl!=1 }">readonly="readonly"</c:if>
                                   onchange="allChange('${buOrgCdNm.key}');"><span>全选</span><br/>
                            <c:forEach items="${sysBizFlowJson[buOrgCdNm.key] }" var="kv" begin="0" step="1"
                                       varStatus="status">
                                <input id="${kv.key }" type="checkbox" value="${kv.key }" name="pDefKey${buOrgCdNm.key}"
                                       <c:if test="${saveBizFlowRl!=1 }">readonly="readonly"</c:if>
                                       onchange="pDefKeyChange('${buOrgCdNm.key}');">
                                <span>${kv.value }
				    		<c:if test="${kv.key=='FLOW_00000001_007_001' or kv.key == 'FLOW_00000014_007_001'  or kv.key == 'FLOW_00000013_007_003' }">
				    			<span id="f_${kv.key }" style="display: none">
					    			<span style="color: red;">支持不上牌资产数量</span>
			                        <input name="flowNum${kv.key }" id="flowNum${kv.key }" type="text" value=""
                                           style="width: 50px"
                                           <c:if test="${saveNoSpNum!=1 }">readonly="readonly"</c:if>>台
			                        <span style="color: red;">超期30天未上牌车辆数量</span>
			                        <input name="expire${kv.key }" id="expire${kv.key }" type="text" value=""
                                           style="width: 50px"
                                           <c:if test="${saveNoSpNum!=1 }">readonly="readonly"</c:if>>台
				    			</span>
                            </c:if>
                            <c:if test="${kv.key=='FLOW_00000001_035_001' }">
                                <span id="f1_${kv.key }" style="display: none">
                                     &nbsp;&nbsp;&nbsp;&nbsp;
                                    <input name="flowNum${kv.key }" id="flowNum${kv.key }" type="text" value=""
                                           onblur="fonselblur();" style="width: 30px;"/>
                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                    <input id="flowNumcheckb" type="checkbox" onchange="onsetNumVal();" value=""
                                           name="flowNumcheckb"/> 不限制
                                    <input id="flowNumcheckbs" name="flowNumcheckbs" type="hidden" value="">
                                </span>
                            </c:if>
			    		</span><br/>
                            </c:forEach>
                        </div>
                    </div>
                </div>
            </c:forEach>
            <div class="fieldBox col_50" style="display: none">
                <div class="label">申请单号</div>
                <div class="field">
                    <a href="javascript:showFlow()" class="link">
                        <span name="pno" id="pno">${pNo}</span>
                    </a>
                </div>
            </div>
            <div class="btnsBox">
                <c:if test="${saveBizFlowRl == 1 and isShowSave == 1}">
                    <span id="saveBtn1" class="sq-button sq-button-round" onclick="add();">保存</span>
                </c:if>
                <input name="dataId" id="dataId" type="hidden" value="${dataId }">
                <input name="tabNm" id="tabNm" type="hidden" value="${tabNm }">
                <input name="codeGrpCd" id="codeGrpCd" type="hidden" value="${codeGrpCd }">
                <input name="currBuOrgCd" id="currBuOrgCd" type="hidden" value="${currBuOrgCd }">
            </div>
        </form>
    </c:if>
</div>

<script type="text/javascript">
    var currBuOrgCd = '${currBuOrgCd}';
    var buOrgCdNm = '${buOrgCdJson}';
    var isShowSave = '${isShowSave}';
    var isFlow = '${isFlow}';
    var sysBizFlowJson = '${sysBizFlowJson}';
    console.log("buOrgCdNm == " + buOrgCdNm);
    $(function () {
        $('#formSet1').ligerForm({
            validate: true
        });
        initData();
        var saveBizFlowRl = '${saveBizFlowRl}';
        if (saveBizFlowRl != 1) {
            $('input[name^="pDefKey"]').setDisabled();
        }
    });

    function add() {
        if(!isShowSave || isShowSave == '0'){
            $.ligerDialog.SQalert({content:"当前事业部无权修改经销商放款流程权限.", SQ_type: "error"});
            return;
        }
        var form = liger.get("formSet1");
        var data = form.getData();
        data["dataId"] = $("#dataId").val();
        data["tabNm"] = $("#tabNm").val();
        data["codeGrpCd"] = $("#codeGrpCd").val();

        var checkBoxResult = checkFlowAuthSelected();
        if (!checkBoxResult['isSuccess']) {
            $.ligerDialog.SQalert({content: checkBoxResult['info'], SQ_type: "warning"});
            return false;
        }
        //获取已经选中的
        /* var pDefKeyString = "";
        $('input[name="pDefKey"]:checked').each(function(){
            var val = $(this).val();
            pDefKeyString = pDefKeyString+";"+val
        });
        data["pDefKeyString"] = pDefKeyString; */

        console.log("=data=" + JSON.stringify(data));
        //alert(JSON.stringify(data));
        var url = '<%=path%>/bizflow/sysBizFlowRl/addAndApply.do?currBuOrgCd='+currBuOrgCd;
        var wait_dialog;
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: url,
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    }

    function initData() {
        var dataId = $("#dataId").val();
        var tabNm = $("#tabNm").val();
        var codeGrpCd = $("#codeGrpCd").val();
        if (!dataId || !tabNm || !codeGrpCd) {
            //chahgeItemStatus(true);
            return;
        }
        var _url = '<%=path%>/bizflow/sysBizFlowRl/getBizFlowRl.do?currBuOrgCd='+currBuOrgCd +'&isFlow='+isFlow;
        $.ajax({
            type: 'get',
            dataType: 'json',
            data: {
                dataId: dataId,
                tabNm: tabNm,
                codeGrpCd: codeGrpCd
            },
            url: _url,
            success: function (msg) {
                if (msg.success == 1) {
                    console.log(JSON.stringify(msg.data));
                    setData(msg.data);//设置数据
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    }

    function setData(data) {
        if (data) {
            /* var array = data.split(";");
            $("input[name='pDefKey']").each(function(){
                var id_ = $(this).attr("id");
                if($.inArray(id_, array)>=0){
                    $(this).ligerGetCheckBoxManager().setValue(true);
                }
            }); */
            for (var i = 0; i < data.length; i++) {
                var sysBizFlowRlVO = data[i];
                var buOrgCdSysBizFlowMap = eval("(" + sysBizFlowJson + ")");
                var flag = false ;
                for(var buOrgCd in buOrgCdSysBizFlowMap){
                    console.log("buOrgCd ==" + buOrgCd);
                    console.log("flowKey ==" + JSON.stringify(buOrgCdSysBizFlowMap[buOrgCd]));
                    if(null != buOrgCdSysBizFlowMap[buOrgCd]){
                        for(var index in buOrgCdSysBizFlowMap[buOrgCd]){
                            console.log("index str ==" + JSON.stringify(buOrgCdSysBizFlowMap[buOrgCd][index]));
                            console.log("index ==" + buOrgCdSysBizFlowMap[buOrgCd][index]);
                            console.log("index.key ==" + buOrgCdSysBizFlowMap[buOrgCd][index].key);
                            if(buOrgCdSysBizFlowMap[buOrgCd][index].key == sysBizFlowRlVO.pdefKey){
                                flag = true ;
                                break ;
                            }
                        }
                        if(flag){
                            break ;
                        }
                    }
                }
                if(!flag){
                    continue;
                }
                $("#" + sysBizFlowRlVO.pdefKey + "").ligerGetCheckBoxManager().setValue(true);
                $("#flowNum" + sysBizFlowRlVO.pdefKey + "").val(sysBizFlowRlVO.flowNum);
                if (sysBizFlowRlVO.pdefKey == "FLOW_00000001_035_001") {
                    if (sysBizFlowRlVO.flowNum == "-1") {
                        $("#flowNumcheckb").ligerGetCheckBoxManager().setValue(true);
                        $("#flowNumFLOW_00000001_035_001").val("");
                        $("#flowNumFLOW_00000001_035_001").attr("disabled", "disabled");
                    }
                }
                if (sysBizFlowRlVO.pdefKey == "FLOW_00000001_007_001") {
                    //var expire = $("#expireFLOW_00000001_007_001").val();
                    if (StringUtils.isNotEmpty(sysBizFlowRlVO.expireUncardNum)) {
                        $("#expire" + sysBizFlowRlVO.pdefKey).val(sysBizFlowRlVO.expireUncardNum);
                    } else {
                        // 去掉默认超期30天未上牌资产数 jiangzg 2019-07-17 15:51:25
                        //$("#expireFLOW_00000001_007_001").val(2);
                    }
                }
                if (sysBizFlowRlVO.pdefKey == "FLOW_00000014_007_001") {
                    //var expire = $("#expireFLOW_00000001_007_001").val();
                    if (StringUtils.isNotEmpty(sysBizFlowRlVO.expireUncardNum)) {
                        $("#expire" + sysBizFlowRlVO.pdefKey).val(sysBizFlowRlVO.expireUncardNum);
                    }
                }
                if (sysBizFlowRlVO.pdefKey == "FLOW_00000013_007_003") {
                    //var expire = $("#expireFLOW_00000001_007_001").val();
                    if (StringUtils.isNotEmpty(sysBizFlowRlVO.expireUncardNum)) {
                        $("#expire" + sysBizFlowRlVO.pdefKey).val(sysBizFlowRlVO.expireUncardNum);
                    }
                }

            }
            // 去掉默认超期30天未上牌资产数 jiangzg 2019-07-17 15:51:25
            /*if (data.length == 0) {
                $("#expireFLOW_00000001_007_001").val(2);
            }*/
        }
        pDefKeyChange();
    }

    //全选按钮
    function allChange(buOrgCd) {
        var key = 'pDefKey' + buOrgCd;
        var allKey = 'ALL' + buOrgCd;
        console.log("all ==" + $("#" + allKey + "")[0].checked);
        if ($("#" + allKey + "")[0].checked) {
            $("input[name='" + key + "']").each(function () {
                $(this).ligerGetCheckBoxManager().setValue(true);
            });
        } else {
            $("input[name='" + key + "']").each(function () {
                $(this).ligerGetCheckBoxManager().setValue(false);
            });
        }
        pDefKeyChange(buOrgCd);
    }

    function showFlow() {
        var pNo = null;
        var _pInstId = null;
        var _audStsCd = null;
        var sysFlowSHDTO = JSON.parse('${sysFlowSH}');
        pNo = sysFlowSHDTO.pNo;
        _pInstId = sysFlowSHDTO.pInstId;
        audStsCd = sysFlowSHDTO.audStsCd;
        var _businessKey = $("#dataId").val();
        if (_pInstId) {
            _formUrl = "/bizflow/sysBizFlowRl/toDetailPage.do?saveBizFlowRl=0" + "@@saveNoSpNum=0@@tabNm=SPL_BSC_INFO@@codeGrpCd=FLW0001@@processInstanceId=" + _pInstId + "@@dataId=";
            var params = "?pInsId=" + _pInstId + "&status=" + _audStsCd + "&businessKey=" + _businessKey + "&formUrl=" + _formUrl;
            Add_navTab('tab_spl_pro_flow_' + _businessKey, '查看流程', "<%=path%>/flow/flow/toFlowShowPageFowBus.do" + params);
        }
    }

    var i = 1;

    function onsetNumVal() {
        //alert($('input[name=flowNumcheckb]:checked', '#flowNumcheckb'));
        //alert();
        console.log($('input[name=flowNumcheckb]:checked', '#flowNumcheckb'))

        i++;
        //var y = $("#flowNumcheckb").val();

        //$("input[name='aaaaa']").each(function(){});
        //var flag =$("#FLOW_00000001_035_001")[0].checked;
        /*  if(!$("#FLOW_00000001_035_001")[0].checked){
              flag = false;//有任意一个未选中 f则被赋值为false
          }*/
        var flag = $("#flowNumcheckb")[0].checked;
        $("#flowNumFLOW_00000001_035_001").removeAttr("disabled");
        if (flag) {
            $("#flowNumFLOW_00000001_035_001").attr("disabled", "disabled");
            $("#flowNumFLOW_00000001_035_001").val("");
            //$("#flowNumFLOW_00000001_035_001").val("-1");
            flag = false;
        } else {
            $("#flowNumFLOW_00000001_035_001").removeAttr("disabled");
            //$("#flowNumFLOW_00000001_035_001").val("");
            flag = true;
        }
    }

    function fonselblur() {
        var val = $("#flowNumFLOW_00000001_035_001").val();
        if (val != "" && val != null) {
            //$("#flowNumcheckb").ligerGetCheckBoxManager().setValue(true);
            //$("#flowNumcheckb").attr("readonly","readonly");
            $("#flowNumcheckb").prop("disabled", true);
            //$("#flowNumcheckb").hide();
        } else {
            $("#flowNumcheckb").prop("disabled", false);
            //$("#flowNumcheckb").show();
        }

    }

    //流程定义key选择动作
    function pDefKeyChange(buOrgCd) {
        if (buOrgCd) {
            var allKey = 'ALL' + buOrgCd;
            var key = 'pDefKey' + buOrgCd;
            var f = true;
            $("input[name='" + key + "']").each(function () {
                if (!$(this)[0].checked) {
                    f = false;//有任意一个未选中 f则被赋值为false
                }
                if ($(this)[0].id == "FLOW_00000001_007_001") {
                    if ($("#FLOW_00000001_007_001")[0].checked) {
                        $("#f_FLOW_00000001_007_001").show();
                    } else {
                        $("#f_FLOW_00000001_007_001").hide();
                    }
                }
                if ($(this)[0].id == "FLOW_00000014_007_001") {
                    if ($("#FLOW_00000014_007_001")[0].checked) {
                        $("#f_FLOW_00000014_007_001").show();
                    } else {
                        $("#f_FLOW_00000014_007_001").hide();
                    }
                }
                if ($(this)[0].id == "FLOW_00000013_007_003") {
                    if ($("#FLOW_00000013_007_003")[0].checked) {
                        $("#f_FLOW_00000013_007_003").show();
                    } else {
                        $("#f_FLOW_00000013_007_003").hide();
                    }
                }

                if ($(this)[0].id == "FLOW_00000001_035_001") {
                    if ($("#FLOW_00000001_035_001")[0].checked) {
                        $("#f1_FLOW_00000001_035_001").show();
                    } else {
                        $("#f1_FLOW_00000001_035_001").hide();
                    }
                }
            });
            if (f) {
                $("#" + allKey + "").ligerGetCheckBoxManager().setValue(true);
            } else {
                $("#" + allKey + "").ligerGetCheckBoxManager().setValue(false);
            }
        } else {
            var map = eval("(" + buOrgCdNm + ")");
            for (var buOrgCd in map) {
                var f = true;
                var allKey = 'ALL' + buOrgCd;
                var key = 'pDefKey' + buOrgCd;
                $("input[name='" + key + "']").each(function () {
                    if (!$(this)[0].checked) {
                        f = false;//有任意一个未选中 f则被赋值为false
                    }
                    if ($(this)[0].id == "FLOW_00000001_007_001") {
                        if ($("#FLOW_00000001_007_001")[0].checked) {
                            $("#f_FLOW_00000001_007_001").show();
                        } else {
                            $("#f_FLOW_00000001_007_001").hide();
                        }
                    }
                    if ($(this)[0].id == "FLOW_00000014_007_001") {
                        if ($("#FLOW_00000014_007_001")[0].checked) {
                            $("#f_FLOW_00000014_007_001").show();
                        } else {
                            $("#f_FLOW_00000014_007_001").hide();
                        }
                    }
                    if ($(this)[0].id == "FLOW_00000013_007_003") {
                        if ($("#FLOW_00000013_007_003")[0].checked) {
                            $("#f_FLOW_00000013_007_003").show();
                        } else {
                            $("#f_FLOW_00000013_007_003").hide();
                        }
                    }

                    if ($(this)[0].id == "FLOW_00000001_035_001") {
                        if ($("#FLOW_00000001_035_001")[0].checked) {
                            $("#f1_FLOW_00000001_035_001").show();
                        } else {
                            $("#f1_FLOW_00000001_035_001").hide();
                        }
                    }
                });
                if (f) {
                    $("#" + allKey + "").ligerGetCheckBoxManager().setValue(true);
                } else {
                    $("#" + allKey + "").ligerGetCheckBoxManager().setValue(false);
                }
            }
        }
    }

    //检查流程选择填写正确性
    function checkFlowAuthSelected() {
        var result = {"isSuccess":true,"info":"success"};
        var map = eval("(" + buOrgCdNm + ")");
        for (var buOrgCd in map) {
            var allKey = 'ALL' + buOrgCd;
            var key = 'pDefKey' + buOrgCd;
            $("input[name='" + key + "']").each(function () {
                if ($(this)[0].id == "FLOW_00000001_007_001") {
                    if ($("#FLOW_00000001_007_001")[0].checked) {
                        if(!$("#flowNumFLOW_00000001_007_001").val() || $("#flowNumFLOW_00000001_007_001").val() == 0 || $("#flowNumFLOW_00000001_007_001").val() == '0'){
                            result['isSuccess'] = false;
                            result['info'] = "【重卡事业部】支持不上牌资产数量必须＞0";
                            return result;
                        }
                        if($("#expireFLOW_00000001_007_001").val() && (parseInt($("#expireFLOW_00000001_007_001").val()) > parseInt($("#flowNumFLOW_00000001_007_001").val()))){
                            result['isSuccess'] = false;
                            result['info'] = "【重卡事业部】支持不上牌资产数量≥超期30天未上牌数量";
                            return result;
                        }
                    }
                }
                if ($(this)[0].id == "FLOW_00000014_007_001") {
                    if ($("#FLOW_00000014_007_001")[0].checked) {
                        if(!$("#flowNumFLOW_00000014_007_001").val() || $("#flowNumFLOW_00000014_007_001").val() == 0 || $("#flowNumFLOW_00000014_007_001").val() == '0'){
                            result['isSuccess'] = false;
                            result['info'] = "【轻卡事业部】支持不上牌资产数量必须＞0";
                            return result;
                        }
                        if($("#expireFLOW_00000014_007_001").val() && (parseInt($("#expireFLOW_00000014_007_001").val()) > parseInt($("#flowNumFLOW_00000014_007_001").val()))){
                            result['isSuccess'] = false;
                            result['info'] = "【轻卡事业部】支持不上牌资产数量≥超期30天未上牌数量";
                            return result;
                        }
                    }
                }
                 if ($(this)[0].id == "FLOW_00000013_007_003") {
                    if ($("#FLOW_00000013_007_003")[0].checked) {
                        if(!$("#flowNumFLOW_00000013_007_003").val() || $("#flowNumFLOW_00000013_007_003").val() == 0 || $("#flowNumFLOW_00000013_007_003").val() == '0'){
                            result['isSuccess'] = false;
                            result['info'] = "【新能源汽车事业部】支持不上牌资产数量必须＞0";
                            return result;
                        }
                        if($("#expireFLOW_00000013_007_003").val() && (parseInt($("#expireFLOW_00000013_007_003").val()) > parseInt($("#flowNumFLOW_00000013_007_003").val()))){
                            result['isSuccess'] = false;
                            result['info'] = "【新能源汽车事业部】支持不上牌资产数量≥超期30天未上牌数量";
                            return result;
                        }
                    }
                }

                if ($(this)[0].id == "FLOW_00000001_035_001") {
                    if ($("#FLOW_00000001_035_001")[0].checked) {
                        if(!$("#flowNumFLOW_00000001_035_001").val() && !$("#flowNumcheckb")[0].checked){
                            result['isSuccess'] = false;
                            result['info'] = "【重卡事业部】准时放款流程权限支持不上牌资产数量必填(非零)!";
                            return result;
                        }
                        if($("#flowNumFLOW_00000001_035_001").val() && ($("#flowNumFLOW_00000001_035_001").val() == 0 || $("#flowNumFLOW_00000001_035_001").val() == '0')){
                            result['isSuccess'] = false;
                            result['info'] = "【重卡事业部】准时放款流程权限支持不上牌资产数量必须大于0!";
                            return result;
                        }
                    }
                }
            });
        }
        return result;
    }

</script>

</body>
</html>
